package com.carhome.ui;

import com.carhome.R;
import com.carhome.ui.base.BaseFragmentActivity;
import com.carhome.ui.fagment.ArticleFragment;
import com.carhome.ui.fagment.CarFragment;
import com.carhome.ui.fagment.ForumFragment;
import com.carhome.ui.fagment.SalesFragment;
import com.carhome.ui.fagment.UserFragment;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends BaseFragmentActivity {
	private ImageView articleImg, forumImg, carImg, salesImg, userImg;
	private TextView articleTxt, forumTxt, carTxt, salesTxt, userTxt;
	private LinearLayout articleBtn, forumBtn, carBtn, salesBtn, userBtn;
	private FragmentManager fragmentManager;

	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initViews();
		setupViews();
	}

	/**
	 * 初始化控件
	 */
	private void initViews() {
		articleImg = (ImageView) findViewById(R.id.myBottom_articleImg);
		forumImg = (ImageView) findViewById(R.id.myBottom_forumImg);
		carImg = (ImageView) findViewById(R.id.myBottom_CarImg);
		salesImg = (ImageView) findViewById(R.id.myBottom_salesImg);
		userImg = (ImageView) findViewById(R.id.myBottom_userImg);

		articleTxt = (TextView) findViewById(R.id.myBottom_articleTxt);
		forumTxt = (TextView) findViewById(R.id.myBottom_forumTxt);
		carTxt = (TextView) findViewById(R.id.myBottom_CarTxt);
		salesTxt = (TextView) findViewById(R.id.myBottom_salesTxt);
		userTxt = (TextView) findViewById(R.id.myBottom_userTxt);

		articleBtn = (LinearLayout) findViewById(R.id.myBottom_articalBtn);
		forumBtn = (LinearLayout) findViewById(R.id.myBottom_forumBtn);
		carBtn = (LinearLayout) findViewById(R.id.myBottom_CarBtn);
		salesBtn = (LinearLayout) findViewById(R.id.myBottom_salesBtn);
		userBtn = (LinearLayout) findViewById(R.id.myBottom_userBtn);

		// 为顶部控件设置监听
		articleBtn.setOnClickListener(myClickListener);
		forumBtn.setOnClickListener(myClickListener);
		carBtn.setOnClickListener(myClickListener);
		salesBtn.setOnClickListener(myClickListener);
		userBtn.setOnClickListener(myClickListener);

		fragmentManager = getSupportFragmentManager();

	}

	/**
	 * 设置初始控件参数
	 */
	private void setupViews() {
		openFragment(fragmentManager, R.id.activity_main_container, ArticleFragment.class);
		restoreBottomView();

		openFragment(fragmentManager, R.id.activity_main_container, ArticleFragment.class);
		restoreBottomView();
		articleImg.setImageResource(R.drawable.nav_meizu_icon_article_c);
		articleTxt.setTextColor(getResources().getColor(R.color.bottom_selected_text_color));
	}

	/**
	 * 设置底部按钮监听
	 */
	OnClickListener myClickListener = new OnClickListener() {
		public void onClick(View v) {
			int id = v.getId();
			switch (id) {
			case R.id.myBottom_articalBtn:
				openFragment(fragmentManager, R.id.activity_main_container, ArticleFragment.class);
				restoreBottomView();
				articleImg.setImageResource(R.drawable.nav_meizu_icon_article_c);
				articleTxt
						.setTextColor(getResources().getColor(R.color.bottom_selected_text_color));
				break;
			case R.id.myBottom_forumBtn:
				openFragment(fragmentManager, R.id.activity_main_container, ForumFragment.class);
				restoreBottomView();
				forumImg.setImageResource(R.drawable.nav_meizu_icon_forum_c);
				forumTxt.setTextColor(getResources().getColor(R.color.bottom_selected_text_color));
				break;
			case R.id.myBottom_CarBtn:
				openFragment(fragmentManager, R.id.activity_main_container, CarFragment.class);
				restoreBottomView();
				carImg.setImageResource(R.drawable.nav_meizu_icon_car_c);
				carTxt.setTextColor(getResources().getColor(R.color.bottom_selected_text_color));
				break;
			case R.id.myBottom_salesBtn:
				openFragment(fragmentManager, R.id.activity_main_container, SalesFragment.class);
				restoreBottomView();
				salesImg.setImageResource(R.drawable.nav_meizu_icon_sale_c);
				salesTxt.setTextColor(getResources().getColor(R.color.bottom_selected_text_color));
				break;
			case R.id.myBottom_userBtn:
				openFragment(fragmentManager, R.id.activity_main_container, UserFragment.class);
				restoreBottomView();
				userImg.setImageResource(R.drawable.nav_meizu_icon_user_c);
				userTxt.setTextColor(getResources().getColor(R.color.bottom_selected_text_color));
				break;
			default:
				break;
			}
		}
	};

	private void restoreBottomView() {
		articleImg.setImageResource(R.drawable.nav_meizu_icon_article);
		articleTxt.setTextColor(getResources().getColor(android.R.color.black));
		forumImg.setImageResource(R.drawable.nav_meizu_icon_forum);
		forumTxt.setTextColor(getResources().getColor(android.R.color.black));
		carImg.setImageResource(R.drawable.nav_meizu_icon_car);
		carTxt.setTextColor(getResources().getColor(android.R.color.black));
		salesImg.setImageResource(R.drawable.nav_meizu_icon_sale);
		salesTxt.setTextColor(getResources().getColor(android.R.color.black));
		userImg.setImageResource(R.drawable.nav_meizu_icon_user);
		userTxt.setTextColor(getResources().getColor(android.R.color.black));
	}
}
